﻿using Abp.Application.Services.Dto;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Microsoft.EntityFrameworkCore;
using Project.Apps.PersonApp.Dto;
using Project.Entities;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Project.Persons
{
    public class PersonAppService : ProjectAppServiceBase, IPersonAppService
    {
        public readonly IRepository<Person> _PersonIRepository; //定义一个仓储类 泛型里放要操作的类


        public PersonAppService(IRepository<Person> PersonIRepositor) // 写带参的构造函数，把参数给仓储
        {
            _PersonIRepository = PersonIRepositor;
        }

        /// <summary>
        /// 添加或修改人员
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> InsertOrUpdatePerson(PersonDto input)
        {
            Person person = null;
            person = await _PersonIRepository.FirstOrDefaultAsync(x => x.Id == input.Id);
            if (person == null)
            {
                person = new Person();
            }
            person.Id = input.Id;
            person.Name = input.Name;
            person.Address = input.Address;
            person.Telephone = input.Telephone;
            person.DeptId = input.DeptId;
            return await _PersonIRepository.InsertOrUpdateAndGetIdAsync(person);
        }

        /// <summary>
        /// 删除人员
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task DeletePerson(EntityDto input)
        {
            await _PersonIRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 获取单个人员
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PersonDto> GetPerson(EntityDto input)
        {
            Person person = await _PersonIRepository.GetAsync(input.Id);
            return ObjectMapper.Map<PersonDto>(person);
        }

        /// <summary>
        /// 获取人员列表(分页)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<PersonDto>> GetPersonPages(PersonFilter input)
        {
            var query = _PersonIRepository.GetAll().WhereIf(!input.Filter.IsNullOrEmpty(), x => x.Name.Contains(input.Filter));
            var total = await query.CountAsync();
            var list = await query.PageBy(input).ToListAsync();
            var dtoList = ObjectMapper.Map<List<PersonDto>>(list);
            return new PagedResultDto<PersonDto>(total, dtoList);
        }



        /*

        /// <summary>
        ///实现添加与修改的接口 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> CreateorInsertPerson(getPersonDto input)
        {
            Person person = null;
            person = await _PersonIRepository.FirstOrDefaultAsync(x => x.Id == input.id);
            if (person == null)
            {
                person = new Person();
            }

            person.Id = input.id;
            person.name = input.name;
            person.address = input.address;
            person.telephone = input.telephone;

            return await _PersonIRepository.InsertOrUpdateAndGetIdAsync(person);
        }

        /// <summary>
        /// 参数entitydto是dto自带的类 里边的就只有一个id属性 方便只需要用到id的时候用 
        /// 因为实体类继承entity类 也不会因为表多而找不到  因为定义的仓储泛型有类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task deletePerson(EntityDto input)
        {
            await _PersonIRepository.DeleteAsync(input.Id);

        }

        public async Task<getPersonDto> oneperson(EntityDto input)
        {
            //这个地方放这个类型出现无法将类型。。隐式转换因为类型不一样 
            //这里的仓储泛型定义的是person 想把它转化为getPersonDto 跟定是不行的  
            //getPersonDto getPersonDto = null;
            //getPersonDto =await _PersonIRepository.GetAsync(input.Id);
            Person person = null;
            person = await _PersonIRepository.FirstOrDefaultAsync(input.Id);
            //return person;
            //if (person == null)
            //{
            //    person = new Person();
            //}
            var Dto = ObjectMapper.Map<getPersonDto>(person);//ObjectMapper是automapper里类型转换的
            return Dto;
            
        }



        public async Task<PagedResultDto<sortpersonDto>> pagegetpereson(getpersonInput input)
        {
            var query = _PersonIRepository.GetAll().WhereIf(!input.Filter.IsNullOrEmpty(), x => x.name.Contains(input.Filter));
            var total = await query.CountAsync();
            var newsTypes = await query.PageBy(input).ToListAsync();

            var newsTypeDtos =ObjectMapper.Map<List<sortpersonDto>>(newsTypes);


            return new PagedResultDto<sortpersonDto>(total, newsTypeDtos);
        }

    */


    }
}
